package objects;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sections.ISection.Section;

public class ReferenceType implements IObject
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1921080437161527446L;
	private String fTypeName;
	private String fTableName;
	private String fId;
	private String fOid;
	private String fName;
	private String fRevision;
	private String fVault;

	public ReferenceType()
	{
		fTypeName = "";
		setTableName("");
		fId = "";
		fOid = "";
		fName = "";
		fRevision = "";
		fVault = "";
	}

	public String getVault()
	{
		return fVault;
	}

	public void setVault(String vault)
	{
		this.fVault = vault;
	}

	public String getId()
	{
		return fId;
	}

	public void setId(String fId)
	{
		this.fId = fId;
	}

	public String getOid()
	{
		return fOid;
	}

	public void setOid(String fOid)
	{
		this.fOid = fOid;
	}

	public String getName()
	{
		return fName;
	}

	public void setName(String fName)
	{
		this.fName = fName;
	}

	public String getRevision()
	{
		return fRevision;
	}

	public void setRevision(String fRevision)
	{
		this.fRevision = fRevision;
	}

	public void setTypeName(String typeName)
	{
		fTypeName = typeName;
	}

	public String getTypeName()
	{
		return fTypeName;
	}

	public void setTableName(String fTableName)
	{
		this.fTableName = fTableName;
	}

	public String getTableName()
	{
		return fTableName;
	}

	@Override
	public String toString()
	{
		return buildReferenceTypeDefinition();
	}

	public String buildReferenceTypeDefinition()
	{
		StringBuffer refTypeDef = new StringBuffer(200);

		refTypeDef.append("type \"");
		refTypeDef.append(fTypeName);
		refTypeDef.append("\" {");
		refTypeDef.append(sNewLine);

		refTypeDef.append("id ");
		refTypeDef.append(fTableName);
		refTypeDef.append("(");
		refTypeDef.append(fId);
		refTypeDef.append(");");
		refTypeDef.append(sNewLine);

		refTypeDef.append("oid ");
		refTypeDef.append(fTableName);
		refTypeDef.append("(");
		refTypeDef.append(fOid);
		refTypeDef.append(");");
		refTypeDef.append(sNewLine);

		refTypeDef.append("name ");
		refTypeDef.append(fName);
		refTypeDef.append(";");
		refTypeDef.append(sNewLine);

		refTypeDef.append("revision ");
		refTypeDef.append(fRevision);
		refTypeDef.append(";");
		refTypeDef.append(sNewLine);

		refTypeDef.append("reference true;");
		refTypeDef.append(sNewLine);

		refTypeDef.append("vault ");
		refTypeDef.append(fVault);
		refTypeDef.append(";");
		refTypeDef.append(sNewLine);
		
		refTypeDef.append("}");
		refTypeDef.append(sNewLine);

		return refTypeDef.toString();
	}

	@Override
	public Section getSectionType()
	{
		return Section.REF_TYPE;
	}

	@Override
	public String getObjectName()
	{
		return fTypeName;
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}

	private void validateState()
	{
		if(fTypeName == null || fTypeName.isEmpty())
		{
			throw new IllegalArgumentException("");
		}
		else if(fTableName == null)
		{
			throw new IllegalArgumentException("");
		}
		else if(fId == null)
		{
			throw new IllegalArgumentException("");
		}
		else if(fOid == null)
		{
			throw new IllegalArgumentException("");
		}
		else if(fName == null)
		{
			throw new IllegalArgumentException("");
		}
		else if(fRevision == null)
		{
			throw new IllegalArgumentException("");
		}
		else if(fVault == null)
		{
			throw new IllegalArgumentException("");
		}
	}
}
